package com.se.controller;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/callback/weixin/open")
public class WxOpenController {
	private String token = "token";
	
	@GetMapping("")
	public String access(@RequestParam String signature,
	                     @RequestParam String timestamp,
	                     @RequestParam String nonce,
	                     @RequestParam String echostr){
		log.info("signature: {};timestamp: {};nonce: {};echostr: {}",signature,timestamp,nonce,echostr);
		List<String> list = new ArrayList<>();
		list.add(token);
		list.add(timestamp);
		list.add(nonce);
		Collections.sort(list);
		log.info(list.toString());
		String s = list.get(0) + list.get(1) + list.get(2);
		s = SecureUtil.sha1(s);
		log.info("s: {}",s);
		if(Objects.equals(s,signature)){
			return echostr;
		}else {
			return String.valueOf(false);
		}
	}
	
//	@GetMapping("auth")
//	public void auth(@RequestParam String code,
//	                 HttpServletResponse response) throws ResultException {
//		log.info("code: {}",code);
//		weixinOpenService.getAccessTokenByCode(code);
//	}
}
